package com.Helper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

import com.database.FlightDB;
import com.database.ReservationDB;

public class ReservationHelper 
{
	private static String username = null;
	private static Connection connection = null;
	private static ArrayList<FlightInfo> flightList = null;
	private static ArrayList<ReservationInfo> reservationList = null;
	private static ArrayList<SeatInfo> seatList = null;
	private static ArrayList<PassInfo> passList = null;
	
	public static void Setup(Connection _connection, String _username) throws SQLException
	{
		connection = _connection;
		username = _username;
		
		PopulateInfo();
	}
	
	private static void PopulateInfo() throws SQLException
	{
		FlightHelper.Setup(connection);
		ReservationDB resDB = new ReservationDB();
		resDB.Setup(connection);
		reservationList = resDB.GetReservationList(username);
		seatList = resDB.GetSeatList(username);
				
		flightList = new ArrayList<FlightInfo>();
		passList = new ArrayList<PassInfo>();
		
		if (reservationList.size() > 0)
		{
			for (int i = 0; i < reservationList.size(); i++)
			{
				String flightID = reservationList.get(i).flightID;
				flightList.add(FlightHelper.GetSelectedFlights(flightID));
			}
			
			for (int j = 0; j < reservationList.size(); j++)
			{
				PassInfo p = new PassInfo();
				int resID = reservationList.get(j).num;
				p.resID = Integer.toString(resID);
				p.leavingTime = flightList.get(j).departureTime;
				p.arrivingTime = flightList.get(j).arrivalTime;
				p.airline = flightList.get(j).airline;
				p.flightID = flightList.get(j).flightID;
				p.seat = reservationList.get(j).seat;
				p.gate = flightList.get(j).gate;
				p.firstName = reservationList.get(j).firstName;
				p.lastName = reservationList.get(j).lastName;
				p.leavingLocation = flightList.get(j).departureLocation;
				p.arrivingLocation = flightList.get(j).arrivalLocation;
				passList.add(p);
			}
		}
		
	}
	
	public static void AdminSetup(Connection _connection, String _username) throws SQLException
	{
		connection = _connection;
		username = _username;
		
		AdminPopulateInfo();
	}
	
	private static void AdminPopulateInfo() throws SQLException
	{
		FlightHelper.Setup(connection);
		ReservationDB resDB = new ReservationDB();
		resDB.Setup(connection);
		reservationList = resDB.GetAllReservationInfo();
		seatList = resDB.GetAllSeatList();	
		
		flightList = new ArrayList<FlightInfo>();
		passList = new ArrayList<PassInfo>();
		
		if (reservationList.size() > 0)
		{
			for (int i = 0; i < reservationList.size(); i++)
			{
				String flightID = reservationList.get(i).flightID;
				flightList.add(FlightHelper.GetSelectedFlights(flightID));
			}
			
			for (int j = 0; j < reservationList.size(); j++)
			{
				PassInfo p = new PassInfo();
				int resID = reservationList.get(j).num;
				p.resID = Integer.toString(resID);
				p.leavingTime = flightList.get(j).departureTime;
				p.arrivingTime = flightList.get(j).arrivalTime;
				p.airline = flightList.get(j).airline;
				p.flightID = flightList.get(j).flightID;
				p.seat = reservationList.get(j).seat;
				p.gate = flightList.get(j).gate;
				p.firstName = reservationList.get(j).firstName;
				p.lastName = reservationList.get(j).lastName;
				p.meal = reservationList.get(j).meal;
				p.leavingLocation = flightList.get(j).departureLocation;
				p.arrivingLocation = flightList.get(j).arrivalLocation;
				passList.add(p);
			}
		}
	}
		
	public static ArrayList<ReservationInfo> GetReservationList() throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		ArrayList<ReservationInfo> list = db.GetReservationList(username);
		return list;
	}
	
	public static void CancelReservation (String resID) throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		db.Delete(resID);
	}
	
	public static ReservationInfo GetSelectedReservation(int resID) throws SQLException
	{
		ReservationInfo info = null;
		
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		info = db.GetSelectedRes(resID);
		
		return info;
	}
	
	public static int GetNumPointsToClaim() throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		int totalPoint = 0;
		ArrayList<ReservationInfo> list = db.GetCompletedList(username);
		
		for (int i = 0; i < list.size(); i++)
		{
			int point = (int)Float.parseFloat(list.get(i).distance);
			totalPoint += point;
		}
		
		return totalPoint;
	}
	
	public static ArrayList<SeatInfo> GetSeatList() throws SQLException
	{
		return seatList;
	}
	
	public static ArrayList<FlightInfo> GetFlightList() throws SQLException
	{
		return flightList;
	}
	
	public static FlightInfo GetSelectedFlight(String flightID) throws SQLException
	{
		FlightInfo info = null;
		
		for (int i = 0; i < flightList.size(); i++)
		{
			if (flightID.equals(flightList.get(i).flightID))
			{
				info = flightList.get(i);
			}
		}
		
		return info;
	}
	
	public static ArrayList<PassInfo> GetPassList() throws SQLException
	{
		return passList;
	}
	
	public static PassInfo GetSelectedPass(String resID) throws SQLException
	{
		PassInfo p = new PassInfo();
		
		for (int i = 0; i < passList.size(); i++)
		{
			if (resID.equals(passList.get(i).resID))
			{				
				p = passList.get(i);	
			}
		}
		
		return p;
	}
	
	public static void UpdateResStatus(String username) throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		db.UpdateResStatus(username);
	}
	
	public static ArrayList<ReservationInfo> GetAllResList() throws SQLException
	{
		ArrayList<ReservationInfo> resList = new ArrayList<ReservationInfo>();
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		
		resList = db.GetAllReservationList();
		
		return resList;
	}
	
	public static ArrayList<ReservationInfo> GetAllResListByFlight(String flightID) throws SQLException
	{
		ArrayList<ReservationInfo> resList = new ArrayList<ReservationInfo>();
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		
		resList = db.GetReservationListByFlight(flightID);
		
		return resList;
	}
	
	public static ArrayList<ReservationInfo> GetCompletedResList() throws SQLException
	{
		ArrayList<ReservationInfo> resList = new ArrayList<ReservationInfo>();
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		
		resList = db.GetCompletedList(username);
		
		return resList;
	}
	
	public static ArrayList<FlightInfo> GetCompletedFlightList() throws SQLException
	{
		ArrayList<ReservationInfo> resList = GetCompletedResList();
		ArrayList<FlightInfo> flightList = new ArrayList<FlightInfo>();
		FlightHelper.Setup(connection);
		
		for (int i = 0; i < resList.size(); i++)
		{
			String flightID = resList.get(i).flightID;
			FlightInfo flightInfo = FlightHelper.GetSelectedFlights(flightID);
			flightList.add(flightInfo);
		}
		
		return flightList;
	}
	
	public static void SetInactive (int resID) throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		db.SetInactive(resID);
	}
	
	public static void ModFirstName(String s, int id) throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		db.ModFirstName(s, id);
	}
	
	public static void ModLastName(String s, int id) throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		db.ModLastName(s, id);
	}
	
	public static void ModSeat(String s, int id) throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		db.ModSeat(s, id);
	}
	
	public static void ModMeal(String s, int id) throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		db.ModMeal(s, id);
	}
	
	public static void ModFlight(String s, int id) throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		db.ModFlight(s, id);
	}
	
	public static void ModPaidPrice(float paidPrice, int resID) throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		db.ModPaidPrice(paidPrice, resID);
	}
	
	public static void ModPaidPoint(int paidPoint, int resID) throws SQLException
	{
		ReservationDB db = new ReservationDB();
		db.Setup(connection);
		db.ModPaidPoint(paidPoint, resID);
	}
	
	
}
